﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class Test2

#Region "Declarations"
    Dim iFormActionOnData As FormActionOnDataEnum
    Dim iDataObject As New InventoryBatchType
    Dim iDataObjectInventory As New InventoryTypeCollection
    Dim iDataObjectStock As New StockItemTypeCollection
    Dim iReCalculationMethodsPushingValue As Boolean = False
#End Region

    Private Sub Test2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Setup_Grid(Inventory_ExtendedDataGridView)

        Dim user As UserType = SecManager.Login("admin", "admin")
        Dim stkMan As New StkManager(user)
        Me.iDataObjectStock = stkMan.Get_StockItem_All_Col
        For Each item As StockItemType In Me.iDataObjectStock
            AddNewInventoryItem(item)
        Next


    End Sub
    'StockItem_SearchBox
    Private Sub StockItem_SearchBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles StockItem_SearchBox.TextChanged
        Dim sitems As Collections.Generic.IEnumerable(Of StockItemType) = Nothing
        If StockItem_SearchBox.Text <> "" Then
            sitems = From sitem As StockItemType In Me.iDataObjectStock _
                         Where (sitem.ItemCode & _
                                sitem.Description & _
                                sitem.Category & _
                                sitem.SubCategory & _
                                sitem.Brand & _
                                sitem.Model & _
                                sitem.Color & _
                                sitem.Manufacturer & _
                                sitem.CostPrice & _
                                sitem.SellingPrice & _
                                sitem.TaxPercentage).ToLower.Contains(StockItem_SearchBox.Text.ToLower)
                            Select sitem _
                            Order By sitem.ItemCode
        Else
            sitems = From sitem As StockItemType In Me.iDataObjectStock _
                         Select sitem _
                         Order By sitem.ItemCode
        End If
        StockItem_SearchBox.PopupListBox.Items.Clear()
        If (sitems IsNot Nothing) AndAlso (sitems.Count > 0) Then
            For i As Integer = 0 To sitems.Count - 1
                Dim stockItem As StockItemType = sitems(i)
                StockItem_SearchBox.PopupListBox.Items.Add(stockItem.ToDetailsListBoxItem)
            Next
        End If

    End Sub
    Private Sub StockItem_SearchBox_SelectedIndexChanged(sender As Object, e As System.EventArgs)
        If Me.StockItem_SearchBox.SelectedItem IsNot Nothing Then
            Dim det As DetailListBoxItem = Me.StockItem_SearchBox.SelectedItem
            If det.DataObject IsNot Nothing Then
                AddItem_ImageButton.Enabled = True
            Else
                AddItem_ImageButton.Enabled = False
            End If
        Else
            AddItem_ImageButton.Enabled = False
        End If
    End Sub
    'Inventory_ExtendedDataGridView
    Private Sub Inventory_ExtendedDataGridView_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Inventory_ExtendedDataGridView.CellValueChanged
        If (e.ColumnIndex <> -1) AndAlso (e.RowIndex <> -1) Then
            Select Case e.ColumnIndex
                Case Is = 5     'new quantity
                    RecalculateGrid_QuantityCell(e.RowIndex)
                Case Is = 8     'cost calculation method
                    RecalculateGrid_CostPriceCell(e.RowIndex)
                Case Is = 9     'new costprice
                    RecalculateGrid_CostPriceCell(e.RowIndex)
                Case Is = 10    'new costpriceafterinventory
                    If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(12).Value) = False) Then
                        RecalculateGrid_SellingPriceCell(e.RowIndex)
                    ElseIf (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(13).Value) = False) Then
                        RecalculateGrid_ProfitMarginCell(e.RowIndex)
                    End If
                Case Is = 11    'current selling price
                    If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(11).Value) = False) Then
                        Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(13).Value = _
                            Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(11).Value
                    End If
                Case Is = 12    'new profitmargin
                    RecalculateGrid_SellingPriceCell(e.RowIndex)
                Case Is = 13    'new sellingprice
                    RecalculateGrid_ProfitMarginCell(e.RowIndex)
            End Select
        End If
    End Sub
    Private Sub StockItems_ExtendedDataGridView_DataError(sender As Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Inventory_ExtendedDataGridView.DataError
        Debug.Print("Data Error in grid........." & e.Exception.ToString)
    End Sub
    Private Sub StockItems_ExtendedDataGridView_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Inventory_ExtendedDataGridView.EditingControlShowing
        If (e.Control IsNot Nothing) AndAlso (TypeOf e.Control Is ComboBox) Then
            Dim cmb As ComboBox = e.Control
            If cmb.Items.Count > 0 Then
                cmb.SelectedIndex = 0
            End If
        End If
    End Sub
    'AddItem_ImageButton
    Private Sub AddItem_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles AddItem_ImageButton.Click
        If Me.StockItem_SearchBox.SelectedItem IsNot Nothing Then
            Dim ditem As DetailListBoxItem = StockItem_SearchBox.SelectedItem
            Dim stockItem As StockItemType = ditem.DataObject
            Dim inventoryItem As New InventoryType(Guid.NewGuid, stockItem)
            inventoryItem.RefType = STK_InventoryMethodsEnum.GRN_Entry
            inventoryItem.RefId = Me.iDataObject.InventoryBatchId
            AddNewInventoryItem(inventoryItem)
            Me.StockItem_SearchBox.SelectedItem = Nothing
        End If
    End Sub


    Private Sub Setup_Grid(datagrid As DataGridView)
        '
        'InventoryId_Column
        '
        Dim InventoryId_Column As New DataGridViewTextBoxColumn
        InventoryId_Column.DataPropertyName = "InventoryId"
        InventoryId_Column.HeaderText = "InventoryId"
        InventoryId_Column.Name = "InventoryId_Column"
        InventoryId_Column.Visible = False
        InventoryId_Column.Width = 85
        datagrid.Columns.Add(InventoryId_Column)
        '
        'StockItemId_Column
        '
        Dim StockItemId_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle27 As New DataGridViewCellStyle()
        DataGridViewCellStyle27.BackColor = System.Drawing.Color.Silver
        StockItemId_Column.DefaultCellStyle = DataGridViewCellStyle27
        StockItemId_Column.DataPropertyName = "StockItemId"
        StockItemId_Column.HeaderText = "StockItemId"
        StockItemId_Column.Name = "StockItemId_Column"
        StockItemId_Column.ReadOnly = True
        StockItemId_Column.Visible = False
        StockItemId_Column.Width = 89
        datagrid.Columns.Add(StockItemId_Column)
        '
        'ItemCode_Column
        '
        Dim ItemCode_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle28 As New DataGridViewCellStyle()
        DataGridViewCellStyle28.BackColor = System.Drawing.Color.Silver
        ItemCode_Column.DefaultCellStyle = DataGridViewCellStyle28
        ItemCode_Column.DataPropertyName = "ItemCode"
        ItemCode_Column.HeaderText = "ItemCode"
        ItemCode_Column.Name = "ItemCode_Column"
        ItemCode_Column.ReadOnly = True
        ItemCode_Column.Width = 77
        datagrid.Columns.Add(ItemCode_Column)
        '
        'Description_Column
        '
        Dim Description_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle29 As New DataGridViewCellStyle()
        DataGridViewCellStyle29.BackColor = System.Drawing.Color.Silver
        Description_Column.DefaultCellStyle = DataGridViewCellStyle29
        Description_Column.DataPropertyName = "Description"
        Description_Column.HeaderText = "Description"
        Description_Column.Name = "Description_Column"
        Description_Column.ReadOnly = True
        Description_Column.Width = 85
        datagrid.Columns.Add(Description_Column)
        '
        'QuantityCurrent_Column
        '
        Dim QuantityCurrent_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle30 As New DataGridViewCellStyle()
        DataGridViewCellStyle30.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle30.Format = "N2"
        DataGridViewCellStyle30.NullValue = "0"
        QuantityCurrent_Column.DefaultCellStyle = DataGridViewCellStyle30
        QuantityCurrent_Column.DataPropertyName = "Quantity"
        QuantityCurrent_Column.HeaderText = "Current Quantity"
        QuantityCurrent_Column.Name = "QuantityCurrent_Column"
        QuantityCurrent_Column.ReadOnly = True
        QuantityCurrent_Column.Width = 99
        datagrid.Columns.Add(QuantityCurrent_Column)
        '
        'QuantityNew_Column
        '
        Dim QuantityNew_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle31 As New DataGridViewCellStyle()
        DataGridViewCellStyle31.Format = "N2"
        DataGridViewCellStyle31.NullValue = "0"
        QuantityNew_Column.DefaultCellStyle = DataGridViewCellStyle31
        QuantityNew_Column.DataPropertyName = "InventoryQuantity"
        QuantityNew_Column.HeaderText = "New Quantity"
        QuantityNew_Column.Name = "QuantityNew_Column"
        QuantityNew_Column.Width = 88
        datagrid.Columns.Add(QuantityNew_Column)
        '
        'QuantityAfterInventory_Column
        '
        Dim QuantityAfterInventory_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle32 As New DataGridViewCellStyle()
        DataGridViewCellStyle32.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle32.Format = "N2"
        DataGridViewCellStyle32.NullValue = "0"
        QuantityAfterInventory_Column.DefaultCellStyle = DataGridViewCellStyle32
        QuantityAfterInventory_Column.HeaderText = "Quantity After Inventory"
        QuantityAfterInventory_Column.Name = "QuantityAfterInventory_Column"
        QuantityAfterInventory_Column.ReadOnly = True
        QuantityAfterInventory_Column.Width = 131
        datagrid.Columns.Add(QuantityAfterInventory_Column)
        '
        'CostPriceCurrent_Column
        '
        Dim CostPriceCurrent_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle33 As New DataGridViewCellStyle()
        DataGridViewCellStyle33.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle33.Format = "N2"
        DataGridViewCellStyle33.NullValue = "0"
        CostPriceCurrent_Column.DefaultCellStyle = DataGridViewCellStyle33
        CostPriceCurrent_Column.DataPropertyName = "CostPrice"
        CostPriceCurrent_Column.HeaderText = "Current Cost Price"
        CostPriceCurrent_Column.Name = "CostPriceCurrent_Column"
        CostPriceCurrent_Column.ReadOnly = True
        CostPriceCurrent_Column.Width = 86
        datagrid.Columns.Add(CostPriceCurrent_Column)
        '
        'CostCalculationMethod_Column
        '
        Dim CostCalculationMethod_Column As New DataGridViewComboBoxColumn
        CostCalculationMethod_Column.HeaderText = "Cost Calculation Method"
        CostCalculationMethod_Column.Items.AddRange(New Object() {"Replace_With_New_Cost", "Moving_Average_Cost"})
        CostCalculationMethod_Column.Name = "CostCalculationMethod_Column"
        CostCalculationMethod_Column.Width = 115
        datagrid.Columns.Add(CostCalculationMethod_Column)
        '
        'CostPriceNew_Column
        '
        Dim CostPriceNew_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle34 As New DataGridViewCellStyle()
        DataGridViewCellStyle34.Format = "N2"
        DataGridViewCellStyle34.NullValue = "0"
        CostPriceNew_Column.DefaultCellStyle = DataGridViewCellStyle34
        CostPriceNew_Column.DataPropertyName = "InventoryCostPrice"
        CostPriceNew_Column.HeaderText = "New Cost Price"
        CostPriceNew_Column.Name = "CostPriceNew_Column"
        CostPriceNew_Column.Width = 97
        datagrid.Columns.Add(CostPriceNew_Column)
        '
        'CostPriceAfterInventory_Column
        '
        Dim CostPriceAfterInventory_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle35 As New DataGridViewCellStyle()
        DataGridViewCellStyle35.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle35.Format = "N2"
        DataGridViewCellStyle35.NullValue = "0"
        CostPriceAfterInventory_Column.DefaultCellStyle = DataGridViewCellStyle35
        CostPriceAfterInventory_Column.HeaderText = "Cost Price After Inventory"
        CostPriceAfterInventory_Column.Name = "CostPriceAfterInventory_Column"
        CostPriceAfterInventory_Column.ReadOnly = True
        CostPriceAfterInventory_Column.Width = 139
        datagrid.Columns.Add(CostPriceAfterInventory_Column)
        '
        'SellingPriceCurrentColumn
        '
        Dim SellingPriceCurrentColumn As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle36 As New DataGridViewCellStyle()
        DataGridViewCellStyle36.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle36.Format = "N2"
        DataGridViewCellStyle36.NullValue = "0"
        SellingPriceCurrentColumn.DefaultCellStyle = DataGridViewCellStyle36
        SellingPriceCurrentColumn.DataPropertyName = "SellingPrice"
        SellingPriceCurrentColumn.HeaderText = "Current Selling Price"
        SellingPriceCurrentColumn.Name = "SellingPriceCurrentColumn"
        SellingPriceCurrentColumn.ReadOnly = True
        SellingPriceCurrentColumn.Width = 95
        datagrid.Columns.Add(SellingPriceCurrentColumn)
        '
        'ProfitMargin_Column
        '
        Dim ProfitMargin_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle37 As New DataGridViewCellStyle()
        DataGridViewCellStyle37.Format = "N2"
        DataGridViewCellStyle37.NullValue = "0"
        ProfitMargin_Column.DefaultCellStyle = DataGridViewCellStyle37
        ProfitMargin_Column.HeaderText = "Profit Margin"
        ProfitMargin_Column.Name = "ProfitMargin_Column"
        ProfitMargin_Column.Width = 84
        datagrid.Columns.Add(ProfitMargin_Column)
        '
        'SellingPrice_Column
        '
        Dim SellingPrice_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle38 As New DataGridViewCellStyle()
        DataGridViewCellStyle38.Format = "N2"
        DataGridViewCellStyle38.NullValue = "0"
        SellingPrice_Column.DefaultCellStyle = DataGridViewCellStyle38
        SellingPrice_Column.DataPropertyName = "InventorySellingPrice"
        SellingPrice_Column.HeaderText = "Selling Price"
        SellingPrice_Column.Name = "SellingPrice_Column"
        SellingPrice_Column.Width = 83
        datagrid.Columns.Add(SellingPrice_Column)
        '
        'TaxPercentage_Column
        '
        Dim TaxPercentage_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle39 As New DataGridViewCellStyle()
        DataGridViewCellStyle39.Format = "N2"
        DataGridViewCellStyle39.NullValue = "0"
        TaxPercentage_Column.DefaultCellStyle = DataGridViewCellStyle39
        TaxPercentage_Column.DataPropertyName = "TaxPercentage"
        TaxPercentage_Column.HeaderText = "Tax Percentage"
        TaxPercentage_Column.Name = "TaxPercentage_Column"
        TaxPercentage_Column.Width = 99
        datagrid.Columns.Add(TaxPercentage_Column)

    End Sub
    Private Sub AddNewInventoryItem(item As StockItemType)

        Inventory_ExtendedDataGridView.DataSource = Nothing
        Dim i As New InventoryType(Guid.NewGuid, item)
        i.RefType = STK_InventoryMethodsEnum.GRN_Entry
        i.RefId = Guid.NewGuid
        Me.iDataObjectInventory.Add(i)
        Inventory_ExtendedDataGridView.AutoGenerateColumns = False
        Inventory_ExtendedDataGridView.DataSource = Me.iDataObjectInventory

    End Sub
    Private Sub RecalculateGrid_QuantityCell(RowIndex As Integer)

        If Me.iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(4).Value) = False) _
                            AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(5).Value) = False) Then
                Dim currentQty As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(4).Value
                Dim newQty As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(5).Value
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(6).Value = currentQty + newQty
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(6).Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
    Private Sub RecalculateGrid_CostPriceCell(RowIndex As Integer)

        If Me.iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(7).Value) = False) _
                           AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(8).Value) = False) _
                           AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(9).Value) = False) Then
                Dim currentCost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(7).Value
                Dim costingMethod As STK_InventoryCostCalculationMethods = _
                    [Enum].Parse(GetType(STK_InventoryCostCalculationMethods), _
                                 Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(8).Value.ToString)
                Dim newCost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(9).Value
                If costingMethod = STK_InventoryCostCalculationMethods.Moving_Average_Cost Then
                    Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value = (currentCost + newCost) / 2
                Else
                    Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value = newCost
                End If
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
    Private Sub RecalculateGrid_SellingPriceCell(RowIndex As Integer)

        If iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value) = False) _
                            AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(12).Value) = False) Then
                Dim margin As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(12).Value
                Dim cost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(13).Value = ((cost * margin) / 100) + cost
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(13).Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
    Private Sub RecalculateGrid_ProfitMarginCell(RowIndex As Integer)

        If Me.iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value) = False) _
                            AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(13).Value) = False) Then
                Dim sellingPrice As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(13).Value
                Dim cost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(10).Value
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(12).Value = (cost / sellingPrice) * 100
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells(12).Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub


End Class